<template>
  <div style="margin-top:-20px;">
    <a-card :bordered="false">
      <div style="display:flex;flex-direction:row">
        <a-button style="margin-top:20px;margin-left:20px" type="primary" @click="addEvent">添加费用</a-button> 
         <a href="http://gangmao.oss-cn-shenzhen.aliyuncs.com/fzydd/excel/费用.xlsx">
          <a-button style="margin: 20px 0 0 20px" type="primary" >
          模板下载
          </a-button>
          </a>
        <a-upload name="file" :multiple="true" :action='uploadFile.url' :headers="headers" :beforeUpload="beforeUpload()" @change="handleChangeupload">
          <a-button style="margin: 20px 0 0 20px" type="primary">
            批量导入
          </a-button>
        </a-upload>
      </div>
      <div class="table-page-search-wrapper" style="background:white;">
        <a-form layout="inline" class="formModel" style="margin-left:20px;margin-top:20px">
          <a-row :gutter="48">
            <a-col :md="4" :sm="24">
              <a-form-item label="合同名称">
                <a-input v-model="queryParam.contractName" placeholder="" />
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="24">
              <a-form-item label="合同编号">
                <a-input v-model="queryParam.contractNo" placeholder="" />
              </a-form-item>
            </a-col>
            <a-col :md="8" :sm="24">
              <a-form-item label="结算时间">
                <a-range-picker :ranges="{ Today: [moment(), moment()], 'This Month': [moment(), moment().endOf('month')] }" v-model="selectTime" @change="onChange" class="rangePicker" />
              </a-form-item>
            </a-col>
            <a-col :md="4" :sm="24">
              <a-button type="primary" @click="selectEvent">查询</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>

      <a-table style="margin: 0 20px 20px 20px" :columns="columnstable" :data-source="data" bordered :loading="tableLoading" :pagination="pagination" @change="pageShow" size="middle">

        <template slot="action" slot-scope="text, record">
          <a @click="modifyEvent(record)">编辑</a>
          <a-divider type="vertical" />
          <a @click="quxiaoEvent(record.id)">删除</a>
        </template>

        <template slot="action1" slot-scope="text, record">
          <a-upload :showUploadList="false" :beforeUpload="fujianbeforeUpload">
            <a style="color:black" @click="sendId(record.id)">
              <a-icon type="upload" />上传附件
            </a>
          </a-upload>
          <a-divider type="vertical" v-if=" record.annex" />
          <a :href="record.annex" v-if="record.annex">下载附件</a>
        </template>
        <template slot="startTime" slot-scope="text, record">
          <span>{{dataFunction(text)}}</span>
        </template>
        <template slot="endTime" slot-scope="text, record">
          <span>{{dataFunction(text)}}</span>
        </template>
      </a-table>
      <!-- -------//进度条----- -->
      <OSSVUE v-if="visiblejindu" v-bind:file="fileData" @uploadSuccessEvent="uploadSuccessEvent"></OSSVUE>
      <!-- //添加费用 -->
      <AddCost v-if="addShow" @cancelEvent="cancelEvent" @successEvent="successEvent"></AddCost>
      <!-- 编辑费用 -->
      <ModifyCost v-bind:info="info" v-if="modifyShow" @cancelEvent="cancelEvent" @successEvent="successEvent"></ModifyCost>
    </a-card>
  </div>
</template>

<script>
import { ACCESS_TOKEN, ROLE_NAME } from '@/store/mutation-types'
import Vue from 'vue'
import { getBase64, UploadToOss } from '@/utils/oss'
import moment from 'moment'
import { getNowTime, getNowFormatDate } from '@/utils/util.js'
import { getManagePagesTotal, uploadingCostAnnex, deleteCost } from '@/api/video'
import AddCost from '@/views/videoManage/costManagement/addCost'
import ModifyCost from '@/views/videoManage/costManagement/modifyCost'
import OSSVUE from '@/views/videoManage/oss'
const data = []
export default {
  components: {
    AddCost,
    ModifyCost,
    OSSVUE,
  },
  data() {
    return {
      fileName: '',
      fileData: '',
      visiblejindu: false,
      modifyShow: false,
      addShow: false,
      tableLoading: false,
      columnstable,
      data,
      info: {},
      searchData: [],
      // create model
      visible: false,
      confirmLoading: false,
      mdl: null,
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      // 加载数据方法 必须为 Promise 对象
      selectedRowKeys: [],
      selectedRows: [],
      current: 1,
      pageSize: 10,
      selectTime: '',
      tableLoading: false,
      pagination: {
        total: 0,
        showTotal: (total) => `共有 ${total} 条数据`,
      },
      uploadFile: {
        id: 1, //非必填 添加项
        name: '',
        url: '/api/import/manage/cost',
        file: '',
        status: '未上传',
        desc: '',
      },
      headers: {
        authorization: Vue.ls.get('ACCESS_TOKEN'),
      },
    }
  },
  filters: {},
  created() {
    let nowtime = getNowFormatDate()
    this.selectTime = [moment(nowtime), moment(nowtime)]
    this.queryParam.startTime = nowtime + ' 00:00:00'
    this.queryParam.endTime = nowtime + ' 23:59:59'
    this.queryParam.current = this.current
    this.queryParam.pageSize = this.pageSize
  },
  computed: {},
  mounted() {
    this.getDataInfoFunction()
  },
  methods: {
    //上传附件
    fujianbeforeUpload(file) {
      if (file != undefined) {
        this.visiblejindu = true
        this.fileData = file
        return false
      }
    },
    // 附件上传成功
    uploadSuccessEvent(data) {
      let name = data.name
      let annex = `http://gangmao.oss-cn-shenzhen.aliyuncs.com/${name}`
      this.visiblejindu = false
      this.uploadingFunction(annex, this.linshiId)
    },
    /**
     * 上传附件api
     */
    uploadingFunction(url, id) {
      uploadingCostAnnex(url, id).then((response) => {
        if (response != undefined) {
          if (response.code == 200) {
            let type = 'success'
            this.$notification[type]({
              message: '温馨提示',
              description: '附件上传成功!',
              duration: 3,
            })
            this.getDataInfoFunction()
          }
        }
      })
    },

    //上传附件
    sendId(id) {
      this.linshiId = id
    },
    //删除费用
    quxiaoEvent(id) {
      this.$confirm({
        title: '提示',
        content: '确定要删除吗?',
        onOk: () => {
          deleteCost(id).then((response) => {
            if (response != undefined) {
              if (response.code == 200) {
                let type = 'success'
                this.$notification[type]({
                  message: '温馨提示',
                  description: '删除成功!',
                  duration: 3,
                })
                this.getDataInfoFunction()
              }
            }
          })
        },
        onCancel() {},
      })
    },
    //编辑费用
    modifyEvent(e) {
      this.info = e
      this.modifyShow = true
    },
    // 添加费用
    addEvent() {
      this.addShow = true
    },
    cancelEvent() {
      this.addShow = false
      this.modifyShow = false
    },
    successEvent() {
      this.getDataInfoFunction()
      this.addShow = false
      this.modifyShow = false
    },
    moment,
    onChange(date, dateString) {
      this.queryParam.startTime = dateString[0] + ' 00:00:00'
      this.queryParam.endTime = dateString[1] + ' 23:59:59'
      this.selectTime = [moment(dateString[0]), moment(dateString[1])]
    },
    //查询
    selectEvent() {
      this.queryParam.current = 1 
      this.getDataInfoFunction()
    },
    //获取表格数据
    getDataInfoFunction() {
      this.tableLoading = true
      getManagePagesTotal(this.queryParam)
        .then((response) => {
          const pagination = { ...this.pagination }
          pagination.total = response.data.total
          pagination.pageSize = this.pageSize
          this.pagination = pagination
          this.pagination.current = response.data.current
          this.data = response.data.records
          this.tableLoading = false
        })
        .catch((error) => {
          this.tableLoading = false
        })
    },
    //分页
    pageShow(pagination) {
      this.tableLoading = true
      this.queryParam.current = pagination.current
      this.queryParam.pageSize = pagination.pageSize
      getManagePagesTotal(this.queryParam)
        .then((response) => {
          const pagination = { ...this.pagination }
          pagination.total = response.data.total
          pagination.pageSize = this.pageSize
          this.pagination = pagination
          this.pagination.current = response.data.current
          this.data = response.data.records
          this.tableLoading = false
        })
        .catch((error) => {
          this.tableLoading = false
        })
    },
    //自定义上传前
    beforeUpload(record) {
      this.headers.authorization = Vue.ls.get('ACCESS_TOKEN')
    },
    //数据导入
    handleChangeupload(info) {
      let fileList = [...info.fileList]
      // this.$message.success('文件正在上传请稍等!')
      if (info.file.status === 'uploading') {
      }
      if (info.file.status === 'done') {
        if (info.file.response.code == 200) {
          this.$message.success(`${info.file.name}文件,上传成功`)
          this.getDataInfoFunction()
        } else {
          this.$message.success(`${info.file.name}文件,上传失败:${info.file.response.msg}`)
        }
      } else if (info.file.status === 'error') {
        this.$message.error(`${info.file.name}文件,上传失败.`)
      } else if (info.file.status === 'removed') {
      }
    },

    dataFunction(dataTime) {
      var date = new Date(dataTime)
      var year = date.getFullYear()
      /* 在日期格式中，月份是从0开始的，因此要加0
       * 使用三元表达式在小于10的前面加0，以达到格式统一  如 09:11:05
       * */
      var month = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
      var day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
      var hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours()
      var minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes()
      var seconds = date.getSeconds() < 10 ? '0' + date.getSeconds() : date.getSeconds()
      return year + '-' + month + '-' + day + ' ' + hours + ':' + minutes
    },
  },
}
const columnstable = [
  {
    title: '合同名称',
    dataIndex: 'contractName',
    width: 100,
    align: 'center',
  },
  {
    title: '合同编号',
    dataIndex: 'contractNo',
    width: 100,
    align: 'center',
  },
  {
    title: '合作单位',
    dataIndex: 'companyName',
    width: 100,
    align: 'center',
  },
  {
    title: '结算开始时间',
    dataIndex: 'startTime',
    width: 100,
    align: 'center',
    scopedSlots: { customRender: 'startTime' },
  },
  {
    title: '结算结束时间',
    dataIndex: 'endTime',
    width: 100,
    align: 'center',
    scopedSlots: { customRender: 'endTime' },
  },
  {
    title: '结算金额',
    dataIndex: 'contractPrice',
    width: 100,
    align: 'center',
  },
  {
    title: '结算说明',
    dataIndex: 'description',
    width: 100,
    align: 'center',
  },
  {
    title: '附件',
    dataIndex: 'annex',
    width: 100,
    align: 'center',
    scopedSlots: { customRender: 'action1' },
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: 100,
    align: 'center',
    scopedSlots: { customRender: 'action' },
  },
]
</script>
